package com.explorati.interview.interview.chapterfifteen.sort;

public class InsertSort<E> implements Sort {

	@Override
	public void sort(Comparable[] arr) {
		for (int i = 1; i < arr.length; i++) {
			E temp;
			for (int j = i; j > 0 && arr[j].compareTo(arr[j - 1]) < 0; j--) {
				temp = (E) arr[j];
				arr[j] = arr[j - 1];
				arr[j - 1] = (Comparable) temp;
			}
		}
	}

	public static void sort(Comparable[] arr, int l, int r) {
		for (int i = l + 1; i <= r; i++) {
			Comparable e = arr[i];
			int j = i;
			for (; j > l && arr[j - 1].compareTo(e) > 0; j--) {
				arr[j] = arr[j - 1];
			}
			arr[j] = e;
		}
		return;
	}

}
